package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.utils.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huang
 * @since 2022-07-05
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 1.校验空参数
     * 2.校验店铺时候存在
     * 3.员工是否存在
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        //校验空参数，自己扩展
       if(StringUtils.isEmpty(shop.getName())){
           throw new CustomException("店铺名称不可为空！");
       }
       //百度ai审核
       if(!BaiduAuditUtils.TextCensor(shop.getName())){
           throw new CustomException("店铺名称不合法，请确认后再试！");
       }
       //校验店铺时候已经存在
        Shop sp = shopMapper.queryByName(shop.getName());
       if(sp != null){
           throw new CustomException("店铺已注册，请确认信息!");
       }
       //校验员工是否存在
        Employee admin = employeeMapper.queryByUserName(shop.getAdmin().getUsername());
        if(admin != null){
            throw new CustomException("员工已注册，请确认信息!");
        }
        //保存员工
        employeeMapper.insert(shop.getAdmin());
        //保存店铺
        shopMapper.insert(shop);
        //更新员工店铺id
        System.out.println(shop.getAdmin().getId());

        shop.getAdmin().setShopId(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) {
        //保存审核日志
        shopAuditLogMapper.insert(shopAuditLog);
        //修改店铺状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        if(shop == null){
            throw new CustomException("数据异常!");
        }
        shop.setState(2);
        shopMapper.update(shop);
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom("410560644@qq.com");
        smm.setTo("hm3761176@vip.qq.com");
        smm.setSubject("今晚打老虎");
        smm.setText("宝塔镇河妖");
        javaMailSender.send(smm);
    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) {
        shopAuditLogMapper.insert(shopAuditLog);
        //修改店铺状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        if(shop == null){
            throw new CustomException("数据异常!");
        }
        shop.setState(4);
        shopMapper.update(shop);
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom("410560644@qq.com");
        smm.setTo("hm3761176@vip.qq.com");
        smm.setSubject("今晚打老虎");
        smm.setText("宝塔镇河妖");
        javaMailSender.send(smm);
    }

    @Override
    public List<Long> statistics() {
        return shopMapper.statistics();
    }
}
